package com.lingkang.flychat.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.lingkang.flychat.R;
import com.lingkang.flychat.entity.ContactsFriendChilds;
import com.lingkang.flychat.entity.ContactsGroup;

import java.util.List;

public class ContactsGroupAdapter extends BaseExpandableListAdapter {
    // 定义的数据
    private List<ContactsGroup> groups;
    private List<ContactsFriendChilds> childs;
    private Context context;

    private GroupViewHolder groupViewHolder;
    private ChildViewHolder childViewHolder;

    public ContactsGroupAdapter(Context context, List<ContactsGroup> groups, List<ContactsFriendChilds> childs) {
        this.context = context;
        this.groups = groups;
        this.childs = childs;
    }

    @Override
    public int getGroupCount() {
        return groups.size();
    }

    @Override
    public int getChildrenCount(int i) {
        return childs.get(i).getContactsFriends().size();
    }

    @Override
    public Object getGroup(int i) {
        return groups.get(i);
    }

    @Override
    public Object getChild(int i, int i1) {
        return childs.get(i);
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    @Override
    //分组和子选项是否持有稳定的ID, 就是说底层数据的改变会不会影响到它们
    public boolean hasStableIds() {
        return true;
    }

    /**
     * 获取显示指定组的视图对象
     */
    @Override
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
/*        if (view == null) {
            view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_contact_group, viewGroup, false);
            groupViewHolder = new GroupViewHolder();
            groupViewHolder.tv_group_name = view.findViewById(R.id.tv_group_name);
            groupViewHolder.iv_right_or_bottom = view.findViewById(R.id.iv_right_or_bottom);
            viewGroup.setTag(groupViewHolder);
        } else {
            groupViewHolder = (GroupViewHolder) view.getTag();
        }*/
        view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_contact_group, viewGroup, false);
        groupViewHolder = new GroupViewHolder();
        groupViewHolder.tv_group_name = view.findViewById(R.id.tv_group_name);
        groupViewHolder.iv_right_or_bottom = view.findViewById(R.id.iv_right_or_bottom);
        viewGroup.setTag(groupViewHolder);
        if (b) {
            groupViewHolder.iv_right_or_bottom.setImageDrawable(ContextCompat.getDrawable(context, R.mipmap.ic_bottom));
        } else {
            groupViewHolder.iv_right_or_bottom.setImageDrawable(ContextCompat.getDrawable(context, R.mipmap.ic_right));
        }
        groupViewHolder.tv_group_name.setText(groups.get(i).getGroupName());
        return view;
    }


    @Override
    public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
        view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_contact_user, viewGroup, false);
        childViewHolder = new ChildViewHolder();
        childViewHolder.iv_img = view.findViewById(R.id.iv_img);
        childViewHolder.tv_name = view.findViewById(R.id.tv_name);
        childViewHolder.tv_autograph = view.findViewById(R.id.tv_autograph);
        //角头像
        Glide.with(context)
                .load(childs.get(i).getContactsFriends().get(i1).getImg())
                .apply(RequestOptions.bitmapTransform(new CircleCrop())) //图片圆形
                .into(childViewHolder.iv_img);
        childViewHolder.tv_name.setText(childs.get(i).getContactsFriends().get(i1).getName());
        childViewHolder.tv_autograph.setText(childs.get(i).getContactsFriends().get(i1).getAutograph());
        return view;
    }

    //指定位置上的子元素是否可选中
    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }

    static class GroupViewHolder {
        TextView tv_group_name;
        ImageView iv_right_or_bottom;
    }

    static class ChildViewHolder {
        ImageView iv_img;
        TextView tv_name;
        TextView tv_autograph;
    }
}
